home *** CD-ROM | disk | FTP | other *** search
/ Aminet 37 / Aminet 37 (2000)(Schatztruhe)[!][Jun 2000].iso / Aminet / comm / bbs / cit_src_AD08.lha / nlstat.c < prev    next >
C/C++ Source or Header  |  1997-07-27  |  12KB  |  348 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5. #include "sysdep.h"
  6. #define UP "System brought up"
  7. #define DOWN "System brought down"
  8. #define NET "System in network mode"
  9. #define NOLOGIN "<No Login>"
  10. #define BAUD300 "300)"
  11. #define BAUD1200 "1200)"
  12. #define BAUD2400 "2400)"
  13. #define BAUD4800 "4800)"
  14. #define BAUD9600 "9600)"
  15. #define BAUD14400 "14400)"
  16. #define BAUD19200 "19200)"
  17. #define CONSOLE "sysConsole)"
  18. #define DEFAULT_READ "calllog.sys"
  19. #define DEFAULT_WRITE "dumbstats"
  20. int main (int , char **);
  21. int main (int argc, char *argv[])
  22.   {
  23.   long calls, net, timeout, error, badword, chat;
  24.   long nologin, restart, calltime[96], nettime[96], tottime[96];
  25.   long duration[1440], baud[8], i, j, k, l, longest, lasthour, atime;
  26.   long lastmin, h1, h2, m1, m2, newuser = 0, stay = 0, busytime[96];
  27.   char firstdate[10], firsttime[10], buffer[80], *name, *date, *starthour,
  28.   *startmin, *stophour, *stopmin, *other, *readname, *writename;
  29.   char lasttime[10],lastdate[10];
  30.   FILE *readfile, *writefile;
  31.  
  32.   calls = net = timeout = error = badword = chat = nologin = 0;
  33.   restart = longest = atime = newuser = stay = 0;
  34.  
  35.   printf ("Citadel log file cruncher %s\n", VERSION_NAME);
  36.   printf ("  by Lobotosoft\n");
  37.   for (i = 0; i < 96; ++i)
  38.     {
  39.     calltime[i] = 0;
  40.     nettime[i] = 0;
  41.     tottime[i] = 0;
  42.     busytime[i] = 0;
  43.  
  44.     }
  45.   for (i = 0; i < 8; ++i)     baud[i] = 0;
  46.   for (i = 0; i < 1440; ++i)  duration[i] = 0;
  47.   writename = DEFAULT_WRITE;
  48.   readname  = DEFAULT_READ;
  49.   if (argc > 2)  readname = argv[2];
  50.   if (argc > 1)  writename = argv[1];
  51.   readfile = fopen (readname, "r");
  52.   if (readfile == NULL)
  53.     {
  54.     printf ("Unable to open log file [%s] for input!\n", readname);
  55.     printf ("Usage: logcrunch [output file] [input file]\n");
  56.     exit (0);
  57.  
  58.     }
  59.   writefile = fopen (writename, "w");
  60.   if (writefile == NULL)
  61.     {
  62.     printf ("Unable to open stats file [%s] for output!\n", writename);
  63.     printf ("Usage: logcrunch [output file] [input file]\n");
  64.     fclose (readfile);
  65.     exit (0);
  66.  
  67.     }
  68.   name      = buffer;
  69.   date      = &buffer[24];
  70.   starthour = &buffer[32];
  71.   startmin  = &buffer[35];
  72.   stophour  = &buffer[40];
  73.   stopmin   = &buffer[43];
  74.   other     = &buffer[47];
  75.   fgets (buffer, 80, readfile);
  76.   while (!feof (readfile))
  77.     {
  78.     if (strncmp (buffer, UP, strlen (UP)) == 0) ++restart;
  79.     if (strlen (buffer) > 23  &&  buffer[22] == ':')
  80.       {
  81.       buffer[22] = 0;
  82.       buffer[31] = 0;
  83.       buffer[37] = 0;
  84.       buffer[42] = 0;
  85.       buffer[45] = 0;
  86.       strcpy (lasttime, starthour);
  87.       strcpy (lastdate, date);
  88.       if (calls == 0  &&  net == 0)
  89.         {
  90.         strcpy (firsttime, starthour);
  91.         strcpy (firstdate, date);
  92.         buffer[34] = 0;
  93.         lasthour = atoi (starthour);
  94.         lastmin = atoi (startmin);
  95.  
  96.         }
  97.       buffer[34] = 0;
  98.       h1 = atoi (starthour);
  99.       h2 = atoi (stophour);
  100.       m1 = atoi (startmin);
  101.       m2 = atoi (stopmin);
  102.       i = h1 * 4 + m1 / 15;
  103.       j = h2 * 4 + m2 / 15;
  104.       k = lasthour * 4 + lastmin / 15;
  105.       tottime[k] += (k + 1) * 15 - lasthour * 60 - lastmin;
  106.       if (j < k)
  107.         {
  108.         for (l = k + 1; l < 96; ++l) tottime[l] += 15;
  109.         for (l = 0; l < j + 1; ++l)  tottime[l] += 15;
  110.         tottime[j] -= (j + 1) * 15 - h2 * 60 - m2;
  111.  
  112.         }
  113.       else
  114.         {
  115.         for (l = k + 1; l < j + 1; ++l) tottime[l] += 15;
  116.         tottime[j] -= (j + 1) * 15 - h2 * 60 - m2;
  117.  
  118.         }
  119.       lasthour = h2;
  120.       lastmin = m2;
  121.       if (strncmp (name, NET, strlen (NET)) == 0)
  122.         {
  123.         ++net;
  124.         ++nettime[i];
  125.         busytime[i] += (i + 1) * 15 - h1 * 60 - m1;
  126.         if (j < i)
  127.           {
  128.           for (l = i + 1; l < 96; ++l) busytime[l] += 15;
  129.           for (l = 0; l < j + 1; ++l)  busytime[l] += 15;
  130.           busytime[j] -= (j + 1) * 15 - h2 * 60 - m2;
  131.  
  132.           }
  133.         else
  134.           {
  135.           for (l = i + 1; l < j + 1; ++l) busytime[l] += 15;
  136.           busytime[j] -= (j + 1) * 15 - h2 * 60 - m2;
  137.  
  138.           }
  139.  
  140.         }
  141.       else
  142.         {
  143.         ++calls;
  144.         ++calltime[i];
  145.         busytime[i] += (i + 1) * 15 - h1 * 60 - m1;
  146.         if (j < i)
  147.           {
  148.           for (l = i + 1; l < 96; ++l) busytime[l] += 15;
  149.           for (l = 0; l < j + 1; ++l)  busytime[l] += 15;
  150.           busytime[j] -= (j + 1) * 15 - h2 * 60 - m2;
  151.  
  152.           }
  153.         else
  154.           {
  155.           for (l = i + 1; l < j + 1; ++l) busytime[l] += 15;
  156.           busytime[j] -= (j + 1) * 15 - h2 * 60 - m2;
  157.  
  158.           }
  159.         l = (h2 - h1) * 60 + m2 - m1;
  160.         if (l < 0)
  161.         l += 1440;
  162.         ++duration[l];
  163.         if (l > longest) longest = l;
  164.         atime += l;
  165.         if (strncmp (NOLOGIN,    name, strlen (NOLOGIN))  == 0) ++nologin;
  166.         if (strncmp (CONSOLE,   other, strlen (CONSOLE))  == 0)++baud[0];
  167.         if (strncmp (BAUD300,   other, strlen (BAUD300))  == 0)++baud[1];
  168.         if (strncmp (BAUD1200,  other, strlen (BAUD1200)) == 0)++baud[2];
  169.         if (strncmp (BAUD2400,  other, strlen (BAUD2400)) == 0)++baud[3];
  170.         if (strncmp (BAUD4800,  other, strlen (BAUD4800)) == 0)++baud[4];
  171.         if (strncmp (BAUD9600,  other, strlen (BAUD9600)) == 0)++baud[5];
  172.         if (strncmp (BAUD14400, other, strlen (BAUD14400))== 0)++baud[6];
  173.         if (strncmp (BAUD19200, other, strlen (BAUD19200))== 0)++baud[7];
  174.         for (l = 0; other[l] != ')'  &&  other[l] != 0; ++l);
  175.         for (;other[l] != 0; ++l)
  176.           {
  177.           if (other[l] == '+')
  178.              ++newuser;
  179.           else if (other[l] == '-')
  180.              ++stay;
  181.           else if (other[l] == 't'  ||  other[l] == 'T')
  182.              ++timeout;
  183.           else if (other[l] == 'e'  ||  other[l] == 'E')
  184.              ++error;
  185.           else if (other[l] == 'b'  ||  other[l] == 'B')
  186.              ++badword;
  187.           else if (other[l] == 'c'  ||  other[l] == 'C')
  188.              ++chat;
  189.  
  190.           }
  191.  
  192.         }
  193.  
  194.       }
  195.     fgets (buffer, 80, readfile);
  196.     printf ("%ld calls, %ld net sessions\r", calls, net);
  197.  
  198.     }
  199.   printf ("\nCrunching data....\n");
  200.   fprintf (writefile, "        System Usage Data\n \n");
  201.   fprintf (writefile, "  Based on usage from  %s, %s to %s, %s\n\n"
  202.   , firstdate,firsttime,lastdate,lasttime);
  203.   fprintf (writefile, "        SYSTEM CALL DATA: \n\n");
  204.   fprintf (writefile, " There were %ld restarts during this period.\n", restart);
  205.   fprintf (writefile, " There were %ld calls, and %ld network sessions\n", calls, net);
  206.   fprintf (writefile, "                     Number   Percent\n");
  207.   fprintf (writefile, " -------------------------------------\n");
  208.   fprintf (writefile, " New Users            %5ld     %3ld\n", newuser, 100 * newuser / calls);
  209.   fprintf (writefile, " No Login Calls       %5ld     %3ld\n", nologin, 100 * nologin / calls);
  210.   fprintf (writefile, " .Terminate Stay      %5ld     %3ld\n", stay,    100 * stay / calls);
  211.   fprintf (writefile, " Chat Attempts        %5ld     %3ld\n", chat,    100 * chat / calls);
  212.   fprintf (writefile, " Timeouts             %5ld     %3ld\n", timeout, 100 * timeout / calls);
  213.   fprintf (writefile, " Exceeded Error Limit %5ld     %3ld\n", error,   100 * error / calls);
  214.   fprintf (writefile, " Badwords usage       %5ld     %3ld\n", badword, 100 * badword / calls);
  215.   fprintf (writefile, " \n    Caller Baud Rates:\n");
  216.   if (baud[1] > 0) fprintf (writefile, "   300 Baud           %5ld     %3ld\n", baud[1], 100 * baud[1] / calls);
  217.   if (baud[2] > 0) fprintf (writefile, "  1200 Baud           %5ld     %3ld\n", baud[2], 100 * baud[2] / calls);
  218.   if (baud[3] > 0) fprintf (writefile, "  2400 Baud           %5ld     %3ld\n", baud[3], 100 * baud[3] / calls);
  219.   if (baud[4] > 0) fprintf (writefile, "  4800 Baud           %5ld     %3ld\n", baud[4], 100 * baud[4] / calls);
  220.   if (baud[5] > 0) fprintf (writefile, "  9600 Baud           %5ld     %3ld\n", baud[5], 100 * baud[5] / calls);
  221.   if (baud[6] > 0) fprintf (writefile, " 14400 Baud           %5ld     %3ld\n", baud[6], 100 * baud[6] / calls);
  222.   if (baud[7] > 0) fprintf (writefile, " 19200 Baud           %5ld     %3ld\n", baud[6], 100 * baud[6] / calls);
  223.   if (baud[0] > 0) fprintf (writefile, " sysConsole           %5ld     %3ld\n", baud[0], 100 * baud[0] / calls);
  224.   fprintf (writefile, " \n \n   CALL DURATION DATA\n \n");
  225.   fprintf (writefile, "    The longest call was %ld minutes,", longest);
  226.   fprintf (writefile, " The average call was %ld minutes.\n \n", atime /  calls);
  227.   for (k = 0, i = 0; k < calls - calls / 25; ++i)  k += duration[i];
  228.   h1 = calls - k;
  229.   longest = i;
  230.   i = i / 50 + 1;
  231.   for (k = 0, m2 = h1; k < longest; ++k)
  232.     {
  233.     for (l = k * i, m1 = 0; l < (k + 1) * i; ++l) m1 += duration[l];
  234.     duration[k] = m1;
  235.     if (m1 > m2) m2 = m1;
  236.  
  237.     }
  238.   j = m2 / 60 + 1;
  239.   fprintf (writefile, " Call duration histogram:\n");
  240.   fprintf (writefile, "  Each bar represents the number of calls of that\n");
  241.   fprintf (writefile, "  duration or up to %ld minutes longer.", i);
  242.   fprintf (writefile, "  Each '=' represents %ld calls.\n \n", j);
  243.   for (k = 0; longest >= k * i; ++k)
  244.     {
  245.     fprintf (writefile, "  %4ld", k * i);
  246.     for (l = 0; l < duration[k] / j; ++l) fprintf (writefile, "=");
  247.     fprintf (writefile, " (%ld)\n", duration[k]);
  248.  
  249.     }
  250.   fprintf (writefile, " >%4ld", --k * i);
  251.   for (l = 0; l < h1 / j; ++l)
  252.   fprintf (writefile, "=");
  253.   fprintf (writefile, " (%ld)\n", h1);
  254.   fprintf (writefile, " \n \n   CALL TIMING DATA\n \n");
  255.   i = 4;
  256.   m1 = 0;
  257.   m2 = 0;
  258.   for (k = 0; k < 24; ++k)
  259.     {
  260.     h1 = 0;
  261.     h2 = 0;
  262.     for (l = k * i; l < (k + 1) * i; ++l)
  263.       {
  264.       h1 += calltime[l];
  265.       h2 += nettime[l];
  266.  
  267.       }
  268.     calltime[k] = h1;
  269.     nettime[k] = h2;
  270.     if (h1 > m1)    m1 = h1;
  271.     if (h2 > m2)    m2 = h2;
  272.  
  273.     }
  274.   h1 = m1 / 60 + 1;
  275.   h2 = m2 / 60 + 1;
  276.   fprintf (writefile, " Call timing histogram:\n");
  277.   fprintf (writefile, "  Each bar represents the number of calls\n");
  278.   fprintf (writefile, "  during that hour.");
  279.   fprintf (writefile, "  Each '=' represents %ld calls.\n", h1);
  280.   for (k = 0; k < 24; ++k)
  281.     {
  282.     fprintf (writefile, " %2ld:00", k);
  283.     for (l = 0; l < calltime[k] / h1; ++l) fprintf (writefile, "=");
  284.     fprintf (writefile, " (%ld)\n", calltime[k]);
  285.  
  286.     }
  287.   fprintf (writefile, "\n Net-Session timing histogram:\n");
  288.   fprintf (writefile, "  Each bar represents the number of net-sessions\n");
  289.   fprintf (writefile, "  during that hour.");
  290.   fprintf (writefile, "  Each '=' represents %ld net-sessions.\n", h2);
  291.   for (k = 0; k < 24; ++k)
  292.     {
  293.     fprintf (writefile, " %2ld:00", k);
  294.     for (l = 0; l < nettime[k] / h2; ++l) fprintf (writefile, "=");
  295.     fprintf (writefile, " (%ld)\n", nettime[k]);
  296.  
  297.     }
  298.   fprintf (writefile, "\n   BEST TIME TO CALL DATA \n");
  299.   for (i = 0, m1 = 0, j = 0; i < 96; ++i)
  300.     {
  301.     if (tottime[i] == 0)
  302.       {
  303.       fprintf (writefile, "  Best time to call data is unavailable,");
  304.       fprintf (writefile, " since there is less than 24 hours of data.\n");
  305.       fclose (writefile);
  306.       fclose (readfile);
  307.       exit (0);
  308.  
  309.       }
  310.     if ((tottime[i] - busytime[i]) * 100 / tottime[i] > m1)
  311.       {
  312.       m1 = (tottime[i] - busytime[i]) * 100 / tottime[i];
  313.       j = i;
  314.  
  315.       }
  316.  
  317.     }
  318.   fprintf (writefile, " The best time to call is around %ld:%02ld,\n", j / 4, (j % 4) * 15);
  319.   fprintf (writefile, "  when there is a %ld percent chance of connecting.\n",  m1);
  320.   i = 4;
  321.   for (m1 = 0, j = 0; j < 24; ++j)
  322.     {
  323.     for (k = j * i, h1 = 0, h2 = 0; k < (j + 1) * i; ++k)
  324.       {
  325.       h1 += tottime[k];
  326.       h2 += busytime[k];
  327.  
  328.       }
  329.     busytime[j] = (h1 - h2) * 100 / h1;
  330.     if (busytime[j] > m1) m1 = busytime[j];
  331.  
  332.     }
  333.   j = m1 / 60 + 1;
  334.   fprintf (writefile, " \n Best Time to Call Graph:\n");
  335.   fprintf (writefile, "  Each bar represents the chance of connecting\n");
  336.   fprintf (writefile, "   during that hour.\n \n");
  337.   for (k = 0; k < 24; ++k)
  338.     {
  339.     fprintf (writefile, " %2ld:00", k);
  340.     for (l = 0; l < busytime[k] / j; ++l)    fprintf (writefile, "=");
  341.     fprintf (writefile, " %ld\n", busytime[k]);
  342.  
  343.     }
  344.   fclose (readfile);
  345.   fclose (writefile);
  346.   return 0;
  347.   }
  348.